//
//  BaseTabBarController.swift
//  Mediaplayer
//
//  Created by bin hu on 2025/7/8.
//

import UIKit

class BaseTabBarController: UITabBarController {
    
    let functionType: FounctionsType
    
    init(functionType: FounctionsType) {
        self.functionType = functionType
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        addAllChildsControllors()

        if #available(iOS 10.0, *) {
            tabBar.theme_unselectedItemTintColor = "Colors.white"
        }
        
        if functionType == .video {
            blackTabBar()
        }else{
            customTabBar()
        }
    }
    
    func addAllChildsControllors() {
        
        switch functionType {
            case .video:
                setupTabbars(with:[HomePageViewController(),ShortPlayerViewController()],
                             titles:["视频","短剧"],
                             norImages: ["",""],
                             selectImages: ["",""])
            case .music:
                setupTabbars(with:[MusicViewController()],
                             titles:["music"],
                             norImages: [""],
                             selectImages: [""])
            case .reader:
                setupTabbars(with:[ReaderViewController(),BookListViewController(),MyBookViewController()],
                             titles:["书城","书城","书架"],
                             norImages: ["","",""],
                             selectImages: ["","",""])
            case .comic:
                setupTabbars(with:[ComicViewController(),ERComicCollectionController()],
                             titles:["comics","collections"],
                             norImages: ["",""],
                             selectImages: ["",""])
            case .download:
                setupTabbars(with:[DownloadViewController()],
                             titles:["download"],
                             norImages: [""],
                             selectImages: [""])
            case .theme:
                setupTabbars(with:[ShortVideoViewController(),ShortPlayerViewController()],
                             titles:["show","theme"],
                             norImages: ["",""],
                             selectImages: ["",""])
            case .topic:
                setupTabbars(with:[ShortVideoViewController(),ShortPlayerViewController()],
                             titles:["chat","firend"],
                             norImages: ["",""],
                             selectImages: ["",""])
            case .dynamic:
                setupTabbars(with:[DynamicDomainsViewController()],
                             titles:["domains"],
                             norImages: [""],
                             selectImages: [""])
            default:
                setupTabbars(with:[DefalutViewController()],
                             titles:["综合"],
                             norImages: [""],
                             selectImages: [""])
        }
    }
    
    func setupTabbars(with vcs:[BaseViewController],
                      titles:[String],
                      norImages:[String],
                      selectImages:[String]) {
        let tabbarItems = CustomTabBar(titles)
        tabbarItems.frame = tabBar.bounds
        tabbarItems.delegate = self
        tabBar.addSubview(tabbarItems)
        for i in 0 ..< vcs.count {
            let vc = vcs[i]
            addOneChildVC(childVC: vc, title: titles[i], imageName: "")
        }
    }
    
    func addOneChildVC(childVC: BaseViewController, title: String?, imageName: String) {
//        childVC.tabBarItem.title = title
        childVC.title = title
//        UIColor.init(hexString: "#13227a")
//        childVC.tabBarItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor:RPColor.redWine], for: .selected)
//        childVC.tabBarItem.image = RPTools.getPngImage(forResource:imageName+"_nor@2x").withRenderingMode(.alwaysOriginal)
//        childVC.tabBarItem.selectedImage = RPTools.getPngImage(forResource:imageName+"_selected@2x").withRenderingMode(.alwaysOriginal)
        let navVC = BaseNavigationController(rootViewController: childVC)
        self.addChild(navVC)
    }
    
    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        for tabBarItem in self.tabBar.subviews {
            if !(tabBarItem is CustomTabBar) {
                tabBarItem.removeFromSuperview()
            }
        }
    }
    
    deinit {
        xlog("🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥")
    }

}

extension BaseTabBarController {
    func blackTabBar() {
        if #available(iOS 15.0, *) {
            let appearance = UITabBarAppearance()
            appearance.configureWithOpaqueBackground()
            appearance.backgroundColor = .black
            appearance.backgroundEffect = nil
            appearance.shadowColor = .clear
            tabBar.standardAppearance = appearance
            tabBar.scrollEdgeAppearance = appearance
        }else{
            //隐藏tabbar上面的黑线
            tabBar.backgroundImage = UIImage(ciImage: .white)
            tabBar.shadowImage = UIImage()
            tabBar.isTranslucent = false
            // tab bar
            tabBar.tintColor = .black
            tabBar.barTintColor = .black
        }
    }
    
    func customTabBar() {
        if #available(iOS 15.0, *) {
            let appearance = UITabBarAppearance()
            appearance.configureWithOpaqueBackground()
            appearance.theme_backgroundColor = "Colors.navBar.backgroundColor"//"Colors.white"
            appearance.backgroundEffect = nil
            appearance.shadowColor = .clear
            tabBar.standardAppearance = appearance
            tabBar.scrollEdgeAppearance = appearance
        }else{
            //隐藏tabbar上面的黑线
            tabBar.backgroundImage = UIImage(ciImage: .white)
            tabBar.shadowImage = UIImage()
            tabBar.isTranslucent = false
            // tab bar
            tabBar.theme_tintColor = "Colors.navBar.backgroundColor"//"Colors.white"
            tabBar.theme_barTintColor = "Colors.navBar.backgroundColor"//"Colors.white"
        }
    }
}

extension BaseTabBarController : RPTabBarwEventDelegate {
    func clickTabBarEventCallBack(_ index: Int) {
        self.selectedIndex = index
        if index == 0 && functionType == .video {
            blackTabBar()
        }else{
            customTabBar()
        }
    }
}
